﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Tools
{
    public class Security
    {



        /// <summary>
        /// 返回 假MD5 加密字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5(string str)
        {
            return Encryption(str, "MD6");
        }

        /// <summary>
        /// 返回 假MD5 解密字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DeMD5(string str)
        {
            return Decryption(str, "MD6");
        }


        /// <summary>
        /// 加密一个字符串
        /// </summary>
        /// <param name="yw">原文</param>
        /// <param name="key">密钥</param>
        /// <returns>返回加密后的字符串</returns>
        private static string Encryption(string yw, string key)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bsmy = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
            TripleDESCryptoServiceProvider t = new TripleDESCryptoServiceProvider();
            t.Key = bsmy;
            t.Mode = CipherMode.ECB;
            byte[] bs = t.CreateEncryptor().TransformFinalBlock(Encoding.UTF8.GetBytes(yw), 0, Encoding.UTF8.GetBytes(yw).Length);
            return Convert.ToBase64String(bs);
        }
        /// <summary>
        /// 解密一个字符串
        /// </summary>
        /// <param name="mw">密文</param>
        /// <param name="key">密钥</param>
        /// <returns>返回原文</returns>
        private static string Decryption(string mw, string key)
        {
            try
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] bsmy = md5.ComputeHash(Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider t = new TripleDESCryptoServiceProvider();
                t.Key = bsmy;
                t.Mode = CipherMode.ECB;
                byte[] bs = t.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(mw), 0, Convert.FromBase64String(mw).Length);
                return Encoding.UTF8.GetString(bs);
            }
            catch (Exception)
            {
                return "";
            }
        }

    }
}
